import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

public class CopyResource {

    private static final String iconPath = "icon";
    private static final String startPath = "start";

    private static final String slideOnePath = "slide/1/";
    private static final String slideTwoPath = "slide/2/";
    private static final String slideThreePath = "slide/3/";

    public static final void main(String[] args) {

        String picPath = "./pic/";
        String resPath = "./vendor/product/res/";
        doCopyRes(picPath, resPath);
    }

    public static void doCopyRes(String picPath, String resPath) {
        try {
            handlePngFile(picPath, iconPath);
            handlePngFile(picPath, startPath);
            handlePngFile(picPath, slideOnePath);
            handlePngFile(picPath, slideTwoPath);
            handlePngFile(picPath, slideThreePath);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("检测图片出现问题");
        }

        try {
            copyPngResource(picPath, resPath);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("复制图片资源出现问题");
        }
        System.out.println("图片资源拷贝成功!");
    }

    /**
     * 将目录下所有签名文件拷贝到工程项目下
     *
     * @param keySourcePath
     * @param keyDestPath
     */
    public static void copyKeyFile(String keySourcePath, String keyDestPath) {
        String[] files = new File(keySourcePath).list();
        if (files == null) {
            return;
        }
        for (String f : files) {
            if (f.toLowerCase().endsWith(".keystore") || f.toLowerCase().endsWith(".jks")) {
                String path = keySourcePath + PackApk.PATH_SEPARATE + f;
                try {
                    copyFile(path, keyDestPath + PackApk.PATH_SEPARATE + f);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void copyUserAgreementFile(String sourcePath, String destPath) {
        String[] files = new File(sourcePath).list();
        if (files == null) {
            return;
        }
        for (String f : files) {
            if (f.toLowerCase().endsWith(".html") || f.toLowerCase().endsWith(".htm")) {
                String path = sourcePath + PackApk.PATH_SEPARATE + f;
                try {
                    copyFile(path, destPath + "/" + f);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void copyAGCPFile(String sourcePath, String destPath) {
        String[] files = new File(sourcePath).list();
        if (files == null) {
            return;
        }
        for (String f : files) {
            if (f.equals("agconnect-services.json")) {
                String path = sourcePath + PackApk.PATH_SEPARATE + f;
                try {
                    copyFile(path, destPath + PackApk.PATH_SEPARATE + f);
                    break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void copyPngResource(String picPath, String resPath) throws IOException {

        mkDrawableDir(resPath);

        copyFile(picPath + iconPath + "/48x48.png", resPath + "drawable-mdpi/ic_launcher.png");
        copyFile(picPath + iconPath + "/72x72.png", resPath + "drawable-hdpi/ic_launcher.png");
        copyFile(picPath + iconPath + "/96x96.png", resPath + "drawable-xhdpi/ic_launcher.png");
        copyFile(picPath + iconPath + "/144x144.png", resPath + "drawable-xxhdpi/ic_launcher.png");

        copyFile(picPath + startPath + "/480x800.png", resPath + "drawable-mdpi/start.png");
        copyFile(picPath + startPath + "/540x960.png", resPath + "drawable-hdpi/start.png");
        copyFile(picPath + startPath + "/720x1280.png", resPath + "drawable-xhdpi/start.png");
        copyFile(picPath + startPath + "/1080x1920.png", resPath + "drawable-xxhdpi/start.png");
//        copyFile(picPath + startPath + "/1080x2160.png", resPath + "drawable-xxhdpi-2160x1080/start.png");

        copyFile(picPath + slideOnePath + "/480x800.png", resPath + "drawable-mdpi/slide_one.png");
        copyFile(picPath + slideOnePath + "/540x960.png", resPath + "drawable-hdpi/slide_one.png");
        copyFile(picPath + slideOnePath + "/720x1280.png", resPath + "drawable-xhdpi/slide_one.png");
        copyFile(picPath + slideOnePath + "/1080x1920.png", resPath + "drawable-xxhdpi/slide_one.png");
//        copyFile(picPath + slideOnePath + "/1080x2160.png", resPath + "drawable-xxhdpi-2160x1080/slide_one.png");

        copyFile(picPath + slideTwoPath + "/480x800.png", resPath + "drawable-mdpi/slide_two.png");
        copyFile(picPath + slideTwoPath + "/540x960.png", resPath + "drawable-hdpi/slide_two.png");
        copyFile(picPath + slideTwoPath + "/720x1280.png", resPath + "drawable-xhdpi/slide_two.png");
        copyFile(picPath + slideTwoPath + "/1080x1920.png", resPath + "drawable-xxhdpi/slide_two.png");
//        copyFile(picPath + slideTwoPath + "/1080x2160.png", resPath + "drawable-xxhdpi-2160x1080/slide_two.png");

        copyFile(picPath + slideThreePath + "/480x800.png", resPath + "drawable-mdpi/slide_three.png");
        copyFile(picPath + slideThreePath + "/540x960.png", resPath + "drawable-hdpi/slide_three.png");
        copyFile(picPath + slideThreePath + "/720x1280.png", resPath + "drawable-xhdpi/slide_three.png");
        copyFile(picPath + slideThreePath + "/1080x1920.png", resPath + "drawable-xxhdpi/slide_three.png");
//        copyFile(picPath + slideThreePath + "/1080x2160.png", resPath + "drawable-xxhdpi-2160x1080/slide_three.png");

    }

    private static void mkDrawableDir(String resPath) {
        mkDirs(resPath);
        mkDirs(resPath + "drawable-mdpi");
        mkDirs(resPath + "drawable-hdpi");
        mkDirs(resPath + "drawable-xhdpi");
        mkDirs(resPath + "drawable-xxhdpi");
        mkDirs(resPath + "drawable-xxhdpi-2160x1080");
    }

    private static boolean mkDirs(String dir) {
        File f = new File(dir);
        if (f.exists()) {
            return true;
        } else {
            return f.mkdirs();
        }
    }

    public static void copyFile(String src, String dest) throws IOException {
        if (!new File(src).exists()) {
            System.out.println("文件" + src + "不存在");
            return;
        }
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);
        byte[] buf = new byte[1024];
        int by = 0;
        while ((by = fis.read(buf)) != -1) {
            fos.write(buf, 0, by);
        }
        fis.close();
        fos.close();
    }

    public static void handlePngFile(String picPath, String imageType) throws IOException {

        File f = new File(picPath + imageType);
        if (!f.exists()) {
            System.out.println(picPath + imageType + " 路径不存在");
            return;
        }

        File[] icons = f.listFiles();
        for (int i = 0; i < icons.length; i++) {

            if (!icons[i].isFile() || !icons[i].getAbsolutePath().toLowerCase().endsWith(".png")) {
                continue;
            }
            FileInputStream fis = new FileInputStream(icons[i]);
            BufferedImage bufferedImg = ImageIO.read(fis);

            int width = bufferedImg.getWidth();
            int height = bufferedImg.getHeight();

            fis.close();
            reNamePng(icons[i], width, height);
        }

    }

    private static void reNamePng(File f, int width, int height) {
        String newName = f.getParent() + "/" + width + "x" + height + ".png";
        f.renameTo(new File(newName));
    }
}
